package mis.ds.ps.model.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import mis.ds.ps.model.dao.MapperDao;
import mis.ds.ps.model.obj.Mapper;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.stereotype.Repository;

import com.felees.hbnpojogen.persistence.impl.GenericHibernateDaoImpl;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 * DAO for table: Mapper.
 * 
 * @author autogenerated
 */
@Repository
public class MapperDaoImpl extends GenericHibernateDaoImpl<Mapper, Long> implements MapperDao {

	private static Logger logger = Logger.getLogger(MapperDaoImpl.class);

	private Connection mysqlConn;

	public static final int ID = 1;
	public static final int MAPPERCOMP_ID = 2;
	public static final int NOME = 3;
	public static final int ENTITY = 4;
	public static final int ATTRIBUTE = 5;
	public static final int MAPVALUE = 6;
	public static final int SHEETINDEX = 7;

	/** Constructor method. */
	public MapperDaoImpl() {
		super(Mapper.class);

		PropertyConfigurator.configure("log4j.properties");
	}

	public List<Mapper> getMappersByCompId(int id) {

		List<Mapper> mappersList = new LinkedList<Mapper>();

		String sql = "SELECT * from mapper where MAPPERCOMP_ID=" + id;
		PreparedStatement prest;
		try {

			prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
			logger.debug("SQL select statement [" + sql + "]");
			ResultSet rs = prest.executeQuery();
			
			
			while (rs.next()) {
				Mapper mp = new Mapper(Long.valueOf(rs.getString(ID)));
				mp.setMappercompId(Long.valueOf(rs.getString(MAPPERCOMP_ID)));
				mp.setNome(rs.getString(NOME));
				mp.setEntity(rs.getString(ENTITY));
				mp.setAttribute(rs.getString(ATTRIBUTE));
				mp.setMapvalue(rs.getString(MAPVALUE));				
				mp.setSheetindex(Integer.valueOf(rs.getString(SHEETINDEX)));
				mappersList.add(mp);
				logger.debug("Adding a mapper ");
			}

		} catch (SQLException e1) {
			logger.debug("Cannot get mappers from DB ", e1);
		}
		
		return mappersList;

	}

	public void setMysqlConn(Connection mysqlConn) {
		this.mysqlConn = mysqlConn;
	}
}
